<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
        <el-form-item label="备注" prop="mark">
          <el-input v-model="formData.mark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as UserBillApi from '@/api/finance/userbill';

export default {
  name: "UserBillForm",
  components: {},
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        mark: undefined,
      },
      // 表单校验
      formRules: {},
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          console.log('获取用户账单数据，ID:', id);
          const res = await UserBillApi.getUserBill(id);
          console.log('获取到的用户账单数据:', JSON.stringify(res.data, null, 2));
          this.formData = res.data;
          this.dialogTitle = "修改用户账单";
        } catch (error) {
          console.error('获取用户账单数据失败:', error);
          this.$modal.msgError("获取数据失败");
        } finally {
          this.formLoading = false;
        }
      } else {
        this.reset();
        this.dialogTitle = "新增用户账单";
      }
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        console.log('提交的表单数据:', JSON.stringify(data, null, 2));
        console.log('最终提交数据', data);
        // 修改的提交
        if (data.id) {
          console.log('执行修改操作，ID:', data.id);
          console.log('提交的mark值:', data.mark);
          // 只发送必要的字段，确保 mark 字段有值
          const updateData = {
            id: data.id,
            mark: data.mark || '' // 确保 mark 字段不为 null
          };
          console.log('实际发送的更新数据:', JSON.stringify(updateData, null, 2));
          const result = await UserBillApi.updateUserBill(updateData);
          console.log('修改接口返回结果:', result);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await UserBillApi.createUserBill(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } catch (error) {
        console.error('提交表单时发生错误:', error);
        this.$modal.msgError("操作失败：" + (error.message || '未知错误'));
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        mark: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
